package com.hongtech.modules.knowledge.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.common.page.PageData;
import com.hongtech.framework.anno.OperateLog;
import com.hongtech.framework.anno.Permission;
import com.hongtech.modules.app.dto.UserDTO;
import com.hongtech.modules.app.service.UserService;
import com.hongtech.modules.common.BaseController;
import com.hongtech.modules.knowledge.dto.AuthDTO;
import com.hongtech.modules.knowledge.entity.AuthEntity;
import com.hongtech.modules.knowledge.service.AuthService;
import java.util.Map;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 认证信息
 *
 * @author lch 123456789@qq.com
 * @since 1.0.0 2023-09-17
 */
@Controller
@RequestMapping("/knowledge/auth")
public class AuthController extends BaseController {

    @Autowired
    private AuthService authService;

    @Autowired
    private UserService userService;

    @Permission("knowledge:auth:list")
    @RequestMapping("/list")
    public String list() {
        return "/knowledge/auth/list";
    }

    @Permission("knowledge:auth:list")
    @RequestMapping("/page")
    @ResponseBody
    public AjaxResultVo page(Integer offset, Integer limit) {
        LambdaQueryWrapper<AuthEntity> ew = new LambdaQueryWrapper<>();
        Map<String, Object> pageParams = getPageParam(limit, offset);
        PageData<AuthDTO> pages = authService.pageAuth(pageParams, ew);
        AjaxResultVo resultVo = AjaxResultVo.success();
        resultVo.put("total", pages.getTotal());
        resultVo.put("rows", pages.getList());
        return resultVo;
    }

    @Permission("knowledge:auth:edit")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        AuthDTO dto = authService.get(id);
        if(ObjectUtils.isNotEmpty(dto)) {
            UserDTO user = userService.get(dto.getUserId());
            dto.setUserName(user.getUsername());
            dto.setAuthStatus(user.getAuthStatus());
        }
        model.addAttribute("entity", dto);
        return "/knowledge/auth/edit";
    }

    @Permission("knowledge:auth:edit")
    @OperateLog("审核认证信息")
    @RequestMapping("/doAuth")
    @ResponseBody
    public AjaxResultVo doEdit(UserDTO dto) {
        if(ObjectUtils.isNotEmpty(dto.getUserId())) {
            userService.update(dto);
        }
        return AjaxResultVo.success();
    }

    @Permission("knowledge:auth:remove")
    @OperateLog("删除认证信息")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResultVo delete(Long id) {
        authService.removeById(id);
        return AjaxResultVo.success();
    }

}
